unit uAppInstance;

interface

uses uAppInstanceInterfaces;

  function AppInstance: IAppInstance;

implementation
uses
  SysUtils, Windows, ActiveX, Classes, ComObj, StdVcl, uAppInstanceCursor, uAppInstanceProcessForm,
  uAppInstanceDialog, uAppInstanceSound, uAppInstanceApplication;

var
  fAppInstance: IAppInstance;

type
  TAppInstance = class (TInterfacedObject, IAppInstance)
  private
    FCursor: ICursor;
    FProcessForm: IProcessForm;
    FDialog: IDialog;
    FSound: ISound;
    FApplicaion: IApplication;
    FUserInfo: IAppUserInfo;

    function Cursor: ICursor;
    function ProcessForm: IProcessForm;
    function Dialog: IDialog;
    function Sound: ISound;
    function Application: IApplication;
  public
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
  end;

function AppInstance: IAppInstance;
begin
  if not Assigned(fAppInstance) then
    fAppInstance := TAppInstance.Create;
  result := fAppInstance;
end;

{ TAppInstance }
procedure TAppInstance.AfterConstruction;
begin
  inherited;
  FCursor := TAppInstanceCursor.Create as ICursor;
  FProcessForm := TProcessForm.Create as IProcessForm;
  FDialog := TAppInstanceDialog.Create as IDialog;
  FApplicaion := TAppInstanceApplication.Create as IApplication;
  FSound := nil;
  FUserInfo := nil;
end;

function TAppInstance.Application: IApplication;
begin
  Result := FApplicaion;
end;

procedure TAppInstance.BeforeDestruction;
begin
  FCursor := nil;
  FProcessForm := nil;
  FDialog := nil;
  FSound := nil;
  FUserInfo := nil;
  inherited;
end;

function TAppInstance.Cursor: ICursor;
begin
  result := FCursor;
end;

function TAppInstance.Dialog: IDialog;
begin
  result := FDialog;
end;

function TAppInstance.ProcessForm: IProcessForm;
begin
  result := FProcessForm;
end;

function TAppInstance.Sound: ISound;
begin
  if not Assigned(FSound) then
    FSound := TAppInstanceSound.Create as ISound;
  result := FSound;
end;

initialization

finalization
  fAppInstance := nil;

end.
